proc dom_flush_head {out} {
	puts $out {<?xml version="1.0" encoding="utf-8"?>}
	puts $out {<ArrayOfDo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">}
}

proc dom_flush_end {out} {
	puts $out {</ArrayOfDo>}
	flush $out
	close $out
}

proc dom_flush_class {out name namespace base interfaces accesses} {
	puts $out {  <Do xsi:type="DoClass">}
	puts $out "    <Name>$name</Name>"
	puts $out "    <Namespace>$namespace</Namespace>"
	puts $out "    <Base>$base</Base>"
	puts $out {    <Interfaces>}
	foreach interface $interfaces {
		puts $out "      <string>$interface</string>"
	}
	puts $out {    </Interfaces>}
	puts $out {    <Access>}
	foreach access $accesses {
		puts $out "      <string>$access</string>"
	}
	puts $out {    </Access>}
	puts $out {  </Do>}
}

proc dom_flush {output} {
	global classes
	global NAMESPACE SUPERCLASS stack_ends stack_namespace stack_superclass

	set out [open $output w]
	dom_flush_head $out
	
	append was [assert $NAMESPACE "" $out]
	append was [assert $SUPERCLASS "" $out]
	append was [assert [count $stack_ends] 0 $out]
	append was [assert [count $stack_namespace] 0 $out]
	append was [assert [count $stack_superclass] 0 $out]
	
	if {$was==""} {
		foreach item $classes(names) {
			set name $classes($item,superclass)
			if {$name!=""} {
				set name $name.$item
			} {
				set name $item
			}
			
			dom_flush_class $out $name $classes($item,namespace) $classes($item,base) $classes($item,interfaces) $classes($item,access)
		}
	}
	
	dom_flush_end $out
}